﻿using CIS.Client.Common.Events;
using CIS.Client.Common.HttpRequestHelper;
using CIS.Common.Dtos;
using CIS.Common.Dtos.ClientDtos;
using CIS.Common.Dtos.ReportDtos;
using CIS.Common.Enums;
using MediatR;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace CIS.Client.Modules.Home.ViewModels
{
    public class HomeViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;
        private log4net.ILog _logger;
        private HttpRequestClient _httpRequest {  get; set; }
        public HomeViewModel(IRegionManager regionManager,IEventAggregator eventAggregator,HttpRequestClient httpRequestClient)
        {
            _regionManager = regionManager;
            eventAggregator.GetEvent<UnitStatusChangeReportEvent>().Subscribe(EventReportHandler);
            _logger = log4net.LogManager.GetLogger("OperationLog");
            _httpRequest = httpRequestClient;
            _logger.Info("主界面已打开");
            Init();
        }

        #region 属性
        private InlineToolClientDto inlineTool;

        public InlineToolClientDto InlineTool
        {
            get { return inlineTool; }
            set { inlineTool = value; }
        }
        private bool remoteBtnIsChecked;

        public bool RemoteBtnIsChecked
        {
            get { return remoteBtnIsChecked; }
            set { remoteBtnIsChecked = value; RaisePropertyChanged(nameof(RemoteBtnIsChecked)); }
        }

        private bool onLineBtnIsChecked;

        public bool OnLineBtnIsChecked
        {
            get { return onLineBtnIsChecked; }
            set { onLineBtnIsChecked = value; RaisePropertyChanged(nameof(OnLineBtnIsChecked)); }
        }

        private bool offLineBtnIsChecked;

        public bool OffLineBtnIsChecked
        {
            get { return offLineBtnIsChecked; }
            set { offLineBtnIsChecked = value; RaisePropertyChanged(nameof(OffLineBtnIsChecked)); }
        }



        private string unitStatusBackGround = "#c26808";

        public string UnitStatusBackGround
        {
            get { return unitStatusBackGround; }
            set { unitStatusBackGround = value; RaisePropertyChanged(nameof(UnitStatusBackGround)); }
        }
        #endregion

        #region 命令

        #endregion

        #region 方法
        private async void Init()
        {
            await Task.Run(async() =>
            {
                await Task.Delay(1000);
                HttpRequest request = new HttpRequest();
                request.Route = "Unit/GetInlineTool";
                RequestResult result = await _httpRequest.SendGetAsync(request);
                InlineTool = JsonConvert.DeserializeObject<InlineToolClientDto>(Convert.ToString(result.Data));
                if (InlineTool != null)
                {
                    switch (InlineTool.ControlState)
                    {
                        case CRST.OffLine:
                            RemoteBtnIsChecked = false;
                            OnLineBtnIsChecked = false;
                            OffLineBtnIsChecked = true;
                            break;
                        case CRST.OnLine:
                            RemoteBtnIsChecked = false;
                            OnLineBtnIsChecked = true;
                            OffLineBtnIsChecked = false;
                            break;
                        case CRST.OnLineRemote:
                            RemoteBtnIsChecked = true;
                            OnLineBtnIsChecked = false;
                            OffLineBtnIsChecked = false;
                            break;
                    }
                }
            });
            
        }
        #endregion



        private void EventReportHandler(UnitStatusChangeReportDto dto)
        {
            var unitstate =Enum.GetName(typeof(EquipmentStatus),dto.UnitStatus);
            if(unitstate == "Run")
            {
                UnitStatusBackGround = "Green";
            }
            if (unitstate == "Idle")
            {
                UnitStatusBackGround = "Yellow";
            }
            if (unitstate == "Maint")
            {
                UnitStatusBackGround = "#c26808";
            }
            if (unitstate == "Down")
            {
                UnitStatusBackGround = "Red";
            }
            _logger.Info($"Received Unit Status Change Report: {unitstate}");
            //MessageBox.Show($"Received Unit Status: {unitstate}");
        }



        #region TabControl 相关
        public string? PageTitle { get; set; } = "首页";

        public string? NavUri { get; set; } = "HomeView";

        public bool IsCanClose { get; set; } = false;

        public DelegateCommand<string>? CloseCommand
        {
            get
            {
                return new DelegateCommand<string>(CloseFunction);
            }
        }

        private void CloseFunction(string navUri)
        {
            //var a = _regionManager.Regions["MainRegion"].ActiveViews.FirstOrDefault();
            //_regionManager.Regions["MainRegion"].Remove(a);
        }



        #endregion
    }
}
